package com.chocus.core.service.mq.listener;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;

import com.chocus.core.bean.product.Color;
import com.chocus.core.bean.product.Product;
import com.chocus.core.bean.product.Sku;
import com.chocus.core.service.cms.CmsService;
import com.chocus.core.service.cms.StaticPageService;

/**
 * mq消息监听器处理类
 * 
 * @author Administrator
 *
 */
public class CustomerMessageListener implements MessageListener {

	@Autowired
	private StaticPageService staticPageService;
	@Autowired
	private CmsService cmsService;

	@Override
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		ActiveMQTextMessage am = (ActiveMQTextMessage) message;
		try {
			String id = am.getText();
			System.out.println(id);
			Map<String, Object> rootMap = new HashMap<String, Object>();
			Product product = cmsService.getProduct(Long.parseLong(id));
			rootMap.put("product", product);
			List<Sku> skus = cmsService.getSkuByProductId(Long.parseLong(id));
			rootMap.put("skus", skus);
			Set<Color> colors = new HashSet<Color>();
			for (Sku sku : skus) {
				colors.add(sku.getColor());
			}
			rootMap.put("colors", colors);
			staticPageService.getStaticPage(id, rootMap);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
